a = int(input('Введите четырёхзначное число: '))
mx = 0
b = 0
x = 0
y = 1
if a > 9999 or a < 1000:
    print(mx)
else:
    for i in range(4):
        for j in range(2, int(str(a)[x:y]) - 1):
            if int(str(a)[x:y]) % j == 0:
                b = 1
                break
        if b == 0 and mx < int(str(a)[x:y]):
            mx = int(str(a)[x:y])
        b = 0
        x += 1
        y += 1
    x = 0
    y = 2
    for i in range(3):
        for j in range(2, int(str(a)[x:y]) - 1):
            if int(str(a)[x:y]) % j == 0:
                b = 1
                break
        if b == 0 and mx < int(str(a)[x:y]):
            mx = int(str(a)[x:y])
        b = 0
        x += 1
        y += 1
    x = 0
    y = 3
    for i in range(2):
        for j in range(2, int(str(a)[x:y]) - 1):
            if int(str(a)[x:y]) % j == 0:
                b = 1
                break
        if b == 0 and mx < int(str(a)[x:y]):
            mx = int(str(a)[x:y])
        b = 0
        x += 1
        y += 1
    x = 0
    y = 4
    for i in range(2, int(str(a)[x:y]) - 1):
        if int(str(a)[x:y]) % 1 == 0:
            b = 1
            break
    if b == 0 and mx < int(str(a)[x:y]):
        mx = int(str(a)[x:y])
    if mx == 1:
        print(0)
    else:
        print(mx)

